/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.gui;

import java.util.List;
import java.util.Random;

import org.openeccos.EccosAppInstance;
import org.openeccos.util.PDUtil;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Font;
import nextapp.echo2.app.ImageReference;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import echopointng.ContainerEx;
import echopointng.LabelEx;
import echopointng.Strut;
import echopointng.able.Scrollable;
import echopointng.xhtml.XhtmlFragment;

/**
 * A panel which shows some structured information
 * May be used in the background for assisting the user, or in
 * the context of a slide-show
 *
 * @author cgspinner@web.de
 */
public class PDInfoPane extends ContainerEx {

	protected final static String LEFT = "left";
	protected final static String RIGHT = "right";
	protected final static int INSET_RANDOM = 0;
	protected final static int INSET_RANDOM_PLUS_X = 1;
	protected final static int INSET_MINIMUM = 2;

	protected LabelEx lblTitle;
	protected LabelEx lblDescription;
	protected LabelEx lblText;
	protected LabelEx lblAttributes;
	protected Button btnArrow;

	public PDInfoPane() {
		this(INSET_RANDOM, false);
	}

	public PDInfoPane(int position) {
		this(position, false);
	}

	public PDInfoPane(int position, boolean scrollableText) {
		initGUI(position, scrollableText);
	}

	private void initGUI(int position, boolean scrollableText) {
		int x = 40;
		int y = 50;
		if (position == INSET_RANDOM) {
			x = 80 + new Random().nextInt(80);
			y += new Random().nextInt(60);
		} else if (position == INSET_RANDOM_PLUS_X) {
			x = 170 + new Random().nextInt(80);
			y += new Random().nextInt(60);
		}

		setInsets(new Insets(x, y, 0, 0));
		lblTitle = new LabelEx();
		lblTitle.setInsets(new Insets(0, 0, 0, 3));
		lblTitle.setForeground(Color.DARKGRAY);
		lblTitle.setFont(new Font(Font.VERDANA, Font.BOLD, new Extent(22)));
		add(lblTitle);

		lblDescription = new LabelEx();
		lblDescription.setOutsets(new Insets(0, 6, 0, 0));
		lblDescription.setInsets(new Insets(3, 3, 0, 3));
		lblDescription.setLineWrap(true);
		lblDescription.setWidth(new Extent(450));
		lblDescription.setForeground(Color.WHITE);
		lblDescription.setBackground(Color.DARKGRAY);
		lblDescription.setFont(new Font(Font.VERDANA, Font.BOLD, new Extent(18)));
		add(lblDescription);

		if (EccosAppInstance.getInstance().isIE()) {
			add(new Strut(0, 8));
		}
		
		lblText = new LabelEx();
		lblText.setTextAlignment(new Alignment(Alignment.LEADING, Alignment.TOP));
		lblText.setInsets(new Insets(0, 0, 0, 0));
		lblText.setLineWrap(true);
		lblText.setWidth(new Extent(450));
		lblText.setForeground(Color.DARKGRAY);
		lblText.setFont(new Font(Font.VERDANA, Font.PLAIN, new Extent(16)));

		if (scrollableText) {
			lblText.setOutsets(new Insets(0, 3, 0, 0));
			ContainerEx scrollContainer = new ContainerEx();
			scrollContainer.setScrollBarPolicy(Scrollable.AUTO);
			scrollContainer.setWidth(new Extent(600));
			scrollContainer.setHeight(new Extent(250));
			add(scrollContainer);
			scrollContainer.add(lblText);
		} else {
			add(lblText);
		}

		lblAttributes = new LabelEx();
		lblAttributes.setInsets(new Insets(0, 0, 0, 0));
		lblAttributes.setLineWrap(true);
		lblAttributes.setWidth(new Extent(320));
		lblAttributes.setForeground(Color.DARKGRAY);
		lblAttributes.setFont(new Font(Font.VERDANA, Font.ITALIC, new Extent(14)));
		add(lblAttributes);
	}

	public void setText(String text) {
		lblText.setText(PDUtil.getXHTML(text));
	}


	public void setAttributes(List<String> attributes) {
		String s = "<span xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"> <xhtml:p><xhtml:ul>";
		for (String att : attributes) {
			s += "<xhtml:li>" + att + "</xhtml:li>";
		}
		s += "</xhtml:ul></xhtml:p> </span>";
		XhtmlFragment xhtml = new XhtmlFragment(s);
		lblAttributes.setText(xhtml);
	}

	protected String getImageTag(String img, String alignment) {
		ImageReference rir = PDUtil.getImage(img);
		LabelEx lbl = new LabelEx(rir);
		lbl.setWidth(new Extent(0));
		lbl.setHeight(new Extent(0));
		add(lbl);

		//XXX use ImageManager instead
		String s = "<xhtml:img  align=\"" + alignment
				+ "\" vspace=\"5\" hspace=\"10\" src=\"/Showroom/?serviceId=Echo.StreamImage&amp;imageuid="
				+ rir.getRenderId() + "\" />";
		return s;
	}

	protected void addArrowButton(String caption) {
		btnArrow = new Button(caption);
		btnArrow.setIcon(PDUtil.getImage("img/orangearrow.gif"));
		btnArrow.setTextPosition(new Alignment(Alignment.CENTER, Alignment.BOTTOM));
		btnArrow.setInsets(new Insets(210, 5, 0, 0));
		btnArrow.setForeground(Color.ORANGE);
		btnArrow.setBackground(null);
		btnArrow.setWidth(new Extent(200));
		btnArrow.setFont(new Font(Font.VERDANA, Font.BOLD, new Extent(14)));
		btnArrow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnArrowClicked();
			}
		});
		add(btnArrow);
	}

	protected void btnArrowClicked() {
		// overwrite if needed
	}

}
